package com.quasar.hpatient.widget;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.change360.calendarview.CalendarDay;
import com.quasar.hpatient.R;
import com.quasar.hpatient.onInterface.OnDateChangedEvent;
import com.rey.material.app.DatePickerDialog;
import com.rey.material.app.DialogFragment;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class WidgetDateQuery extends LinearLayout implements View.OnClickListener {
    public WidgetDateQuery(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }
    public void setDate(CalendarDay calendarDay){
        selectCalendar=calendarDay.getCalendar();
        date.setText(format.format(selectCalendar.getTime()));
    }

    //获取当前选定的时间
    public String getDate(){

        return date.getText().toString();
    }

    public void setDate(String TimeDate){
        date.setText(TimeDate);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.widget_date_query, this);
        pre = (Button) findViewById(R.id.pre);
        next = (Button) findViewById(R.id.next);
        date = (TextView) findViewById(R.id.date);
        iv_search_date= (ImageView) findViewById(R.id.iv_search_date);
        pre.setOnClickListener(this);
        next.setOnClickListener(this);
        iv_search_date.setOnClickListener(this);
        selectCalendar = Calendar.getInstance();
        date.setText(format.format(selectCalendar.getTime()));
    }

    Calendar selectCalendar;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Button pre;
    Button next;
    TextView date;
    ImageView iv_search_date;

    private String Status;

    public String getStatus() {
        return Status;
    }

    public void setStatus(String status) {
        Status = status;
    }

    public WidgetDateQuery(Context context) {
        super(context);
        init(context);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.pre) {
            setStatus("1");
            next.setEnabled(true);
            selectCalendar.add(Calendar.DAY_OF_MONTH, -1);
            date.setText(format.format(selectCalendar.getTime()));

            if (onDateChangedEvent != null) {
                    onDateChangedEvent.onDateChanged(new CalendarDay(selectCalendar));
            }
        } else if (v.getId() == R.id.next) {
             setStatus("2");
            selectCalendar.add(Calendar.DAY_OF_MONTH, 1);
            Calendar now=Calendar.getInstance();
            now.set(Calendar.HOUR_OF_DAY, 23);
            if (selectCalendar.after(now)){
                selectCalendar.add(Calendar.DAY_OF_MONTH, -1);
                next.setEnabled(false);
                return;
            }else{
                next.setEnabled(true);
            }
            date.setText(format.format(selectCalendar.getTime()));

            if (onDateChangedEvent != null) {
                onDateChangedEvent.onDateChanged(new CalendarDay(selectCalendar));
            }
        } else if (v.getId() == R.id.iv_search_date) {
            showSelectDate();
        }
    }

    private void showSelectDate() {
        DatePickerDialog.Builder builder = null;
        builder = new DatePickerDialog.Builder() {
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                DatePickerDialog dialog = (DatePickerDialog) fragment.getDialog();
                Calendar temp = dialog.getCalendar();
                selectCalendar = temp;
                date.setText(format.format(selectCalendar.getTime()));
                if (onDateChangedEvent != null) {
                    if (!selectCalendar.equals(temp)){
                        onDateChangedEvent.onDateChanged(new CalendarDay(temp));
                    }
                }
                super.onPositiveActionClicked(fragment);
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                super.onNegativeActionClicked(fragment);
            }
        };
        Long  min=System.currentTimeMillis()-1000*60*60*24*365*10l;
        Long  max=System.currentTimeMillis();
        builder.dateRange(min,max);
        builder.date(selectCalendar.getTimeInMillis());
        builder.positiveAction("确认")
                .negativeAction("取消");

        if (getContext() instanceof AppCompatActivity) {
            AppCompatActivity activity = (AppCompatActivity) getContext();
            DialogFragment fragment = DialogFragment.newInstance(builder);
            fragment.show(activity.getSupportFragmentManager(), null);
        }
    }
    private OnDateChangedEvent onDateChangedEvent;

    public OnDateChangedEvent getOnDateChangedEvent() {
        return onDateChangedEvent;
    }

    public void setOnDateChangedEvent(OnDateChangedEvent onDateChangedEvent) {
        this.onDateChangedEvent = onDateChangedEvent;
    }
}
